package com.coffee.admin.service.merchant.impl;

import com.coffee.admin.api.merchant.query.MerchantIntegralLogQuery;
import com.coffee.admin.api.merchant.result.MerchantIntegralLogResult;
import com.coffee.admin.api.merchant.service.MerchantIntegralLogService;
import com.coffee.admin.service.merchant.bean.MerchantIntegralLog;
import com.coffee.admin.service.merchant.dao.MerchantIntegralLogDao;
import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.ExceptionEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.util.BeanUtilExt;
import com.coffee.core.util.DTOUtils;
import com.coffee.core.util.StringUtil;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.*;


@DubboService(dynamic = true)
public class MerchantIntegralLogServiceImpl implements MerchantIntegralLogService {
	@Resource
	private MerchantIntegralLogDao	merchantIntegralLogDao;

	@Override
	public Page<MerchantIntegralLogResult> getPager(MerchantIntegralLogQuery param, PagerInfo pr) {
		Page<MerchantIntegralLogResult> pager=new Page<MerchantIntegralLogResult>(pr.getStart(), pr.getPageSize());
		Integer count=merchantIntegralLogDao.count(param);
		pager.setTotalPage(count);
		if(count>0) {
			List<MerchantIntegralLog> list=merchantIntegralLogDao.page(param, pr.getStart(), pr.getPageSize());
			pager.setData(DTOUtils.convertList(list, MerchantIntegralLogResult.class));
		}
		return pager;
	}
	
	@Override
	public Integer count(MerchantIntegralLogQuery param){
		Integer count=merchantIntegralLogDao.count(param);
		return count;
	}
	
	@Override
	public MerchantIntegralLogResult getDetail(Integer id){
		MerchantIntegralLog item=merchantIntegralLogDao.findById(id);
		if(item==null){
			return null;
		}
		MerchantIntegralLogResult result=new MerchantIntegralLogResult();
		BeanUtilExt.copyProperties(result, item);
		
		return result;
	}

	@Override
	public List<MerchantIntegralLogResult> getList(MerchantIntegralLogQuery param, PagerInfo pr){
		List<MerchantIntegralLog> list=merchantIntegralLogDao.page(param, pr.getStart(), pr.getPageSize());
		return DTOUtils.convertList(list, MerchantIntegralLogResult.class);
	}
	
	@Override
	public List<MerchantIntegralLogResult> getList(MerchantIntegralLogQuery param){
		List<MerchantIntegralLog> list=merchantIntegralLogDao.page(param, 0, Integer.MAX_VALUE);
		return DTOUtils.convertList(list, MerchantIntegralLogResult.class);
	}
	
	@Override
	public List<MerchantIntegralLogResult> getList(){
		MerchantIntegralLogQuery param = new MerchantIntegralLogQuery();
		List<MerchantIntegralLog> list=merchantIntegralLogDao.page(param, 0, Integer.MAX_VALUE);
		return DTOUtils.convertList(list, MerchantIntegralLogResult.class);
	}


	@Override
	public void delete(Integer id) {
		merchantIntegralLogDao.deleteById(id);
	}

	@Override
	public void addOrUpdate(MerchantIntegralLogResult result)throws BusinessException {
		MerchantIntegralLog item=new MerchantIntegralLog();
		BeanUtilExt.copyProperties(item, result);
		if(item.getId()==null) {
			item.setCreateTime(new Date());
			merchantIntegralLogDao.insert(item);
		}else {
			MerchantIntegralLog tmp=merchantIntegralLogDao.findById(item.getId());
			if(tmp==null) {
				throw new BusinessException(ExceptionEnum.ERROR_0002.getCode(), ExceptionEnum.ERROR_0002.getMessage());
			}
			merchantIntegralLogDao.updateById(item);
		}
	}
	
	@Override
	public Map<Integer,MerchantIntegralLogResult> getItemMap(Set<Integer> idSet){
		Map<Integer, MerchantIntegralLogResult> map=new HashMap<Integer, MerchantIntegralLogResult>();
		List<MerchantIntegralLog> list=merchantIntegralLogDao.findByIds(StringUtil.setToList(idSet));
		List<MerchantIntegralLogResult> resultList=DTOUtils.convertList(list, MerchantIntegralLogResult.class);
		for(MerchantIntegralLogResult item:resultList) {
			map.put(item.getId(), item);
		}
		return map;
	}
	
	@Override
	public Map<Integer,MerchantIntegralLogResult> getItemMap(){
		Map<Integer, MerchantIntegralLogResult> map=new HashMap<Integer, MerchantIntegralLogResult>();
		MerchantIntegralLogQuery param=new MerchantIntegralLogQuery();
		List<MerchantIntegralLog> list=merchantIntegralLogDao.page(param, 0, Integer.MAX_VALUE);
		List<MerchantIntegralLogResult> resultList=DTOUtils.convertList(list, MerchantIntegralLogResult.class);
		for(MerchantIntegralLogResult item:resultList) {
			map.put(item.getId(), item);
		}
		return map;
	}	
	
	
	
}
